<template>
  <div class="page">
    <v-pageTitle title="新增人员"></v-pageTitle>
    
    <el-row type="flex" justify="space-around" style="margin-top:20px;">
      <el-col :span="11">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="ruleForm.userName"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="ruleForm.gender">
              <el-radio label="男"></el-radio>
              <el-radio label="女"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="手机号" prop="tel">
            <el-input v-model="ruleForm.tel"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email"></el-input>
          </el-form-item>
          <el-form-item label="部门" prop="branch">
            <!--<el-input v-model="ruleForm.branch"></el-input>-->
            <el-select v-model="ruleForm.branch" filterable placeholder="请选择">
              <el-option
                v-for="item in branch_options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="岗位" prop="post">
            <!--<el-input v-model="ruleForm.post"></el-input>-->
            <el-select v-model="ruleForm.post" filterable placeholder="请选择">
              <el-option
                v-for="item in post_options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-switch on-text="" off-text="" v-model="ruleForm.status"></el-switch>
          </el-form-item>
          <el-form-item label="备注" prop="mark">
            <el-input type="textarea" v-model="ruleForm.mark"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button  type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
            <el-button  @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
  export default {
    data() {
      // 验证手机号码
      var checkTel = (rule, value, callback) => {
        if(value){
          setTimeout(() => {
            if (!(/^1[34578]\d{9}$/.test(value))) {
              callback(new Error('请填写正确的手机号码格式'));
            } else {
              callback();
            }
          }, 100);
        }else{
          callback();
        }
      };
      // 验证邮箱
      var checkEmail = (rule, value, callback) => {
        if(value){
          setTimeout(() => {
            if (!(/^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g.test(value))) {
              callback(new Error('请填写正确的邮箱格式'));
            } else {
              callback();
            }
          }, 100);
        }else{
          callback();
        }
      };

      return {
        ruleForm: {
          userName: '',
          name: '',
          gender: '',
          tel: '',
          email: '',
          branch: '',
          post: '',
          status: false,
          mark: ''
        },
        branch_options: [{
          value: '选项1',
          label: '黄金糕'
        }, {
          value: '选项2',
          label: '双皮奶'
        }, {
          value: '选项3',
          label: '蚵仔煎'
        }, {
          value: '选项4',
          label: '龙须面'
        }, {
          value: '选项5',
          label: '北京烤鸭'
        }],
        post_options: [{
          value: '选项1',
          label: '黄金糕'
        }, {
          value: '选项2',
          label: '双皮奶'
        }, {
          value: '选项3',
          label: '蚵仔煎'
        }, {
          value: '选项4',
          label: '龙须面'
        }, {
          value: '选项5',
          label: '北京烤鸭'
        }],
        rules: {
          userName: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '请选择性别', trigger: 'change' }
          ],
          tel: [
            { required: false, validator: checkTel, trigger: 'change' }
          ],
          email: [
            { required: false, validator: checkEmail, trigger: 'change' }
          ],
          branch: [
            { required: true, message: '请选择所属部门', trigger: 'change' }
          ],
          post: [
            { required: true, message: '请选择岗位', trigger: 'change' }
          ],
          mark: [
            { required: false, message: '请填写备注', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    mounted() {
    }
  }
</script>